package com.ruibang.glass.purchase.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.common.constants.RedisKey;
import com.ruibang.glass.purchase.domain.req.PurchaseSpecialApplicationReq;
import com.ruibang.glass.purchase.entity.PurchaseSpecialApplication;
import com.ruibang.glass.purchase.mapper.PurchaseSpecialApplicationMapper;
import com.ruibang.glass.purchase.service.PurchaseSpecialApplicationService;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 特采跟踪 服务实现类
 * </p>
 *
 * @author mameng
 * @since 2024-04-29
 */
@Service
public class PurchaseSpecialApplicationServiceImpl extends ServiceImpl<PurchaseSpecialApplicationMapper, PurchaseSpecialApplication> implements PurchaseSpecialApplicationService {

    @Autowired
    private RedisSingleService redisService;
    @Override
    public IPage<PurchaseSpecialApplication> getPage(PurchaseSpecialApplicationReq purchaseSpecialApplicationReq) {
        LambdaQueryWrapper<PurchaseSpecialApplication> queryWrapper = Wrappers.<PurchaseSpecialApplication>lambdaQuery();
        if (StringUtils.isNotEmpty(purchaseSpecialApplicationReq.getItemName())) {
            queryWrapper.like(PurchaseSpecialApplication::getItemName, purchaseSpecialApplicationReq.getItemName());
        }
        if (StringUtils.isNotEmpty(purchaseSpecialApplicationReq.getDepartmentId())) {
            queryWrapper.eq(PurchaseSpecialApplication::getDepartmentId, purchaseSpecialApplicationReq.getDepartmentId());
        }
        if (StringUtils.isNotEmpty(purchaseSpecialApplicationReq.getProjectName())) {
            queryWrapper.like(PurchaseSpecialApplication::getProjectName, purchaseSpecialApplicationReq.getProjectName());
        }
        if (StringUtils.isNotEmpty(purchaseSpecialApplicationReq.getProjectNumber())) {
            queryWrapper.like(PurchaseSpecialApplication::getProjectNumber, purchaseSpecialApplicationReq.getProjectNumber());
        }
        Page<PurchaseSpecialApplication> page = baseMapper.selectPage(new Page<>(purchaseSpecialApplicationReq.getPageNo(), purchaseSpecialApplicationReq.getPageSize()),
                queryWrapper.orderByDesc(PurchaseSpecialApplication::getUpdateTime)
        );
        Map<Object,Object> deptMap = redisService.hmget(RedisKey.DEPT_GROUP_TYPE);
        if(page!=null&& CollectionUtils.isNotEmpty(page.getRecords())){
            for (PurchaseSpecialApplication record : page.getRecords()) {
                record.setDepartmentName(deptMap.get(record.getDepartmentId())==null?
                        null:deptMap.get(record.getDepartmentId()).toString());
            }
        }

        return page;
    }

    @Override
    public List<PurchaseSpecialApplication> getByArrivalOrderId(String id) {
        if (StringUtils.isNotEmpty(id)){
            return lambdaQuery().eq(PurchaseSpecialApplication::getArrivalOrderId,id).list();
        }
        return Collections.emptyList();
    }
}
